package com.yunjie.service;

import java.util.HashSet;
import java.util.Set;

import com.yunjie.model.tree.NodeModel;
import com.yunjie.model.tree.TreeModel;
/**
 * 树形结构帮助类，凡是具有树形接口的对象业务处理，均可继承该类并组织树形结构信息
 * @author yoyo
 *
 * @param <T> TreeModel接口实现类
 */
public class TreeHelper<T extends TreeModel<?>> {
	/**
	 * 得到当前节点的子节点
	 * 该方法只返回当前节点的下一级节点信息，且不加载下级子节点的子节点信息
	 * @param t TreeModel借口的实现类
	 * @return Set<NodeModel>
	 */
	protected Set<NodeModel> getOneChildNodes(TreeModel<?> t) {
		Set<NodeModel> nodes = new HashSet<NodeModel>(0);
		if (!t.getChildrens().isEmpty()) {
			for (TreeModel<?> node : t.getChildrens()) {
				NodeModel n = new NodeModel();
				n.setText(node.getName());
				n.setId(node.getId());
				if (node.getChildrens().isEmpty()) {
					n.setLeaf(true);
				}
				n.setLeaf(false);
				nodes.add(n);
			}
			return nodes;
		}
		return nodes;
	}
	/**
	 * 返回当前节点的树形节点信息，该方法加载当前节点下所有的子节点信息，并组织成树形结构
	 * @param t TreeModel接口实现类
	 * @return 当前节点的树形节点信息
	 */
	protected NodeModel getCurrentNodeWithChildrens(TreeModel<?> t) {
		if (t == null) {
			return null;
		}
		NodeModel currentNode = new NodeModel();
		currentNode.setId(t.getId());
		currentNode.setText(t.getName());
		if (!t.getChildrens().isEmpty()) {
			currentNode.setLeaf(false);
			currentNode.setChildren(this.getAllChildrensNode(t));
		} else {
			currentNode.setLeaf(true);
		}
		return currentNode;
	}
	/**
	 * 返回当前节点的树形节点信息，该方法不加载当前节点下所有的子节点信息
	 * @param t TreeModel接口实现类
	 * @return 当前节点的树形节点信息
	 */
	protected NodeModel getCurrentNodeWithOutChildrens(TreeModel<?> t) {
		if (t == null) {
			return null;
		}
		NodeModel currentNode = new NodeModel();
		currentNode.setId(t.getId());
		currentNode.setText(t.getName());
		if (!t.getChildrens().isEmpty()) {
			currentNode.setLeaf(false);
		} else {
			currentNode.setLeaf(true);
		}
		return currentNode;
	}
	/**
	 * 该方法返回当前节点下的所有树形子节点信息，并组织成树形结构
	 * @param t TreeModel实现类
	 * @return Set<NodeModel>
	 */
	protected Set<NodeModel> getAllChildrensNode(TreeModel<?> t) {
		if (t == null || t.getChildrens().isEmpty()) {
			return null;
		} else {
			Set<NodeModel> nodes = new HashSet<NodeModel>(0);
			for (TreeModel<?> node : t.getChildrens()) {
				NodeModel nodeModel = new NodeModel();
				nodeModel.setId(node.getId());
				nodeModel.setText(node.getName());
				if (!node.getChildrens().isEmpty()) {
					nodeModel.setLeaf(false);
					nodeModel.setChildren(this.getAllChildrensNode(node));
				} else {
					nodeModel.setLeaf(true);
				}
				nodes.add(nodeModel);
			}
			return nodes;
		}
	}
	/**
	 * 得到当前层次具有树形节点树形信息的节点信息
	 * @param ts Set<TreeModel<?>>
	 * @return Set<NodeModel>
	 */
	protected Set<NodeModel> getCurrentNodes(Set<T> ts) {
		Set<NodeModel> nodeModels = new HashSet<NodeModel>(0);
		for(TreeModel<?> node : ts){
			NodeModel nodeModel = new NodeModel();
			nodeModel = this.getCurrentNodeWithChildrens(node);
			nodeModels.add(nodeModel);
		}
		return nodeModels;
	}
}
